<?php

namespace App\Service\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Brick\Math\RoundingMode;

class OrderDetails extends \Eloquent
{

    const status_default = 1;
    const status_success = 2;
    const status_failed  = 3;

    const type_pay      = 1; //支付类型
    const type_withdraw = 2; //提现类型

    

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The database used by the model.
     *
     * @var string
     */
    protected $table = 'order_details';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [''];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [''];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
    }
    

    public function order(){
        return $this->belongsTo(Orders::class,'order_id');
    }
    

    public static function createOrderDetails($bill_amount , $card , $pay_date ,$order_id ,$max_amount ){
        $amount_array         = getRandomNumber($bill_amount , $max_amount);
        // $pay_fee              = $bill_amount * 0.008;
        // $withdraw_fee         = count($amount_array) * 0.3;
        // $total_fee            = $pay_fee + $withdraw_fee;
        $execute_time_array   = [
            Carbon::tomorrow()->setHour(rand(10,12))->setMinute(rand(1,50))->setSecond(rand(1,59)),
            Carbon::tomorrow()->setHour(rand(13,15))->setMinute(rand(1,50))->setSecond(rand(1,59)),
            Carbon::tomorrow()->setHour(rand(16,20))->setMinute(rand(1,50))->setSecond(rand(1,59)),
        ];
        $k = 0;
        $j = 0;
        $pay = $withdraw =array();
        foreach($amount_array as $eachAmount){
            if($j > 0){
                $pay_fee = $withdraw_fee = $total_fee = 0;
            }
            if($k==3){
               $execute_time_array[0]->addDay(1)->setHour(rand(10,12))->setMinute(rand(1,50))->setSecond(rand(1,59));
               $execute_time_array[1]->addDay(1)->setHour(rand(13,15))->setMinute(rand(1,50))->setSecond(rand(1,59));
               $execute_time_array[2]->addDay(1)->setHour(rand(16,20))->setMinute(rand(1,50))->setSecond(rand(1,59));
               $k=0;
            }
            $execute_time = $execute_time_array[$k];
            $pay[]     = [
                'order_id'      => $order_id,
                'agency_id'     => $card->agency_id,
                'order_no'      => generate_orderId('P'),
                'cardno'        => $card->cardno,
                'type'          => OrderDetails::type_pay,
                'amount'        => $eachAmount,
                'agreeid'       => $card->agreeid,
                'cusid'         => $card->cusid,
                'fee'           => toAmount(rmb($eachAmount)->multipliedBy('0.008',RoundingMode::DOWN)),
                'execute_time'  => $execute_time->toDateTimeString(),
                'created_at'    => Carbon::now(),
                'updated_at'    => Carbon::now()
            ];
            
            $execute_time->addHour(1)->setMinute(rand(1,59))->setSecond(rand(1,59));
            
            $withdraw[] = [
                'order_id'      => $order_id,
                'agency_id'     => $card->agency_id,
                'order_no'      => generate_orderId('P'),
                'cardno'        => $card->cardno,
                'type'          => OrderDetails::type_withdraw,
                'amount'        => round($eachAmount - round($eachAmount*0.008,2),2),
                'agreeid'       => $card->agreeid,
                'cusid'         => $card->cusid,
                'fee'           => 1,
                'execute_time'  => $execute_time->toDateTimeString(),
                'created_at'    => Carbon::now(),
                'updated_at'    => Carbon::now()
            ];
            
            $k++;
            $j++;
        }
      
        $all  =  array_merge($pay,$withdraw);
       
        $lastExecuteTime = Carbon::create(end($all)['execute_time']);
        
        $pay_date  = explode('-',$pay_date);
        $pay_date  = Carbon::now()->setMonth($pay_date[0])->setDay($pay_date[1])->setHour(18)->setMinute(0)->setSecond(0);
        
       
        if($lastExecuteTime->gte($pay_date)){
            return false;
        }
        
        return self::addAll($all);

    }

    public static function addAll(Array $data)
    {
        $rs = DB::table('order_details')->insert($data);
        return $rs;
    }

    
    public function getCardnoAttribute($value)
    {
        return hide_middle($value);
    }
    
}
